package com.kit.common.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 公共加解密方法工具类
 * @Description com.kit.common.utils
 * @Author Kit
 * @Date 2024-05-16 23:41
 * @Version 1. 0
 **/

public class CretUtil {
    public static String calculateSHA1(String input) {
        try {
            // 获取SHA-1算法的MessageDigest实例
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            // 将输入字符串转换为字节数组并计算哈希值
            byte[] hashInBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // 将字节数组转换为十六进制格式的字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashInBytes) {
                sb.append(String.format("%02x", b));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }



    public static String calculateMD5(String input) {
        try {
            // 获取MD5算法的MessageDigest实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 将输入字符串转换为字节数组并计算哈希值
            byte[] hashInBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // 将字节数组转换为十六进制格式的字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashInBytes) {
                sb.append(String.format("%02x", b));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

    }
}
